package com.haiyou.common.util.code;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

/**
 * 随机码
 *
 */
public class RandomCode {

	private static List<Character> list = new ArrayList<>();
	static {
		for (int i = 0; i < 10; i++) {
			list.add(new Character((char) ('0' + i)));
		}
		for (int i = 0; i < 26; i++) {
			list.add(new Character((char) ('A' + i)));
		}

		System.out.println(list);
	}

	public static void main(String[] args) {
		randomCode(2000000, 6);
	}

	
	public static String randomACode(int codeLength) {
		String c = "";

		for (int j = 0; j < codeLength; j++) {
			c = c + list.get(new Random().nextInt(list.size()));
		}
		return c;
	}
	
	/**
	 * 随机码生成器
	 * 
	 * @param size       生成数量
	 * @param codeLength 随机码长度
	 */
	public static void randomCode(int size, int codeLength) {
		int count = 0;
		HashSet<String> code = new HashSet<>();
		
		for (int i = 0; i < size; i++) {
			
			count++;

			String c = randomACode(codeLength);
		
			if (code.contains(c)) {
				i = i - 1;
				continue;
			} else {
				code.add(c);
			}
		}
		
		System.out.println(count);
		System.out.println(code.size());
		System.exit(1);
	}
}
